/*
 * RBFTest_Download.c
 *
 *  Created on: 2023年7月22日
 *      Author: mcealt_DS
 * 
 * RFM -> HUB  CMD：0x08 网关下发
 * RFM <- HUB  CMD：0x09 节点应答
 */
#include "RBFDownload.h"
#include "app_log.h"
#include <string.h>

#define CONFIG_LOG_RBFTEST_DOWNLOAD // 模块日志宏定义,使能,模块输出日志

typedef enum
{
  RBFDOWNLOAD_STEP_HUB_TO_RFM,
  RBFDOWNLOAD_STEP_RFM_TO_HUB_ACK,
  RBFDOWNLOAD_STEP_FINISH,
} RBFDownloadtest_step_t;

static RBFDownloadtest_step_t step = RBFDOWNLOAD_STEP_HUB_TO_RFM;
static uint8_t packet[256];

void RBFDownloadtest_LOG( void )
{
#ifdef CONFIG_LOG_RBFTEST_DOWNLOAD
  switch (step)
  {
  case RBFDOWNLOAD_STEP_HUB_TO_RFM:
    DS_app_log_debug("RBFDOWNLOAD_STEP is HUB_TO_RFM:\r\n");
    break;
  case RBFDOWNLOAD_STEP_RFM_TO_HUB_ACK:
    DS_app_log_debug("RBFDOWNLOAD_STEP is RFM_TO_HUB_ACK:\r\n");
    break;
  case RBFDOWNLOAD_STEP_FINISH:
    DS_app_log_debug("RBFDOWNLOAD_STEP is FINISH:\r\n");
    break;
  default:
    break;
  }
#endif
}

static uint8_t kayvalue_buf[] = {0xA1, 0xA2, 0xA3, 0xA4};
void RBFDownload_test(void)
{
  RBFDownloadtest_LOG();
  switch (step)
  {
  case RBFDOWNLOAD_STEP_HUB_TO_RFM:
  {
    RBFMacMessageGWDownload_t msg_rec; 
    RBFMacMessageGWDownload_t msg = {//测试msg
        .macAdd = 0x1234,
        .msgSeq = 0x02,
        .msgRetry = 0x03,
        .rssi = 0x04,
        .qrfH1 = 0x05,
        .qrfH2 = 0x06,
        .keyValueSize = 4,
        .keyValue = kayvalue_buf,
    };
    memset(packet, 0, sizeof(packet));
    RBF_gw_download_payload_msgToPacket(msg, packet);
    RBF_gw_download_payload_packetToMsg(packet, &msg_rec);
    if((memcmp(&msg_rec, &msg, sizeof(RBFMacMessageGWDownload_t) - sizeof(uint8_t*)) == 0) &&
       (memcmp(msg_rec.keyValue, msg.keyValue, (unsigned int)msg.keyValueSize)== 0))
      DS_app_log_debug("GWDownload check ok!\r\n");
    else
    {
        DS_app_log_error("GWDownload check error!\r\n");
    }
    step++;
    break;
  }
  case RBFDOWNLOAD_STEP_RFM_TO_HUB_ACK:
  {
    RBFMacMessageNodeAck_t msg_rec; 
    RBFMacMessageNodeAck_t msg = {//测试msg.
        .battV = 0x01,
        .rssi = 0x02,
        .temp = 0x03,
        .qrfR1 = 0x04,
        .qrfR2 = 0x05,
        .keyValueSize = 4,
        .keyValue = kayvalue_buf,
    };
    
    memset(packet, 0, sizeof(packet));
    RBF_node_ack_payload_msgToPacket(msg, packet);
    RBF_node_ack_payload_packetToMsg(packet, &msg_rec);
    if((memcmp(&msg_rec, &msg, sizeof(RBFMacMessageNodeAck_t) - sizeof(uint8_t*)) == 0) &&
       (memcmp(msg_rec.keyValue, msg.keyValue, (unsigned int)msg.keyValueSize)== 0))
      DS_app_log_debug("NodeAck check ok!\r\n");
    else
      DS_app_log_error("NodeAck check error!\r\n");
    step++;
    break;
  }
  case RBFDOWNLOAD_STEP_FINISH:
  {
    step = 0xff;
    break;
  }
  default:
    break;
  }
}
